package com.sunnuwell.demo.controller;

import com.alibaba.fastjson.JSON;
import com.sunnuwell.demo.annotation.UserLoginToken;
import com.sunnuwell.demo.common.exception.AdException;
import com.sunnuwell.demo.constant.Constants;
import com.sunnuwell.demo.entity.Equipment;
import com.sunnuwell.demo.service.EquipmentService;
import com.sunnuwell.demo.vo.EquipmentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * @program: entry
 * @description: 设备管理
 * @author: Mr.lv
 * @create: 2019-04-26 11:44
 **/
@Slf4j
@RestController
@RequestMapping("/api/v1")
public class EquipmentController {

    private EquipmentService equipmentService;

    public EquipmentController(EquipmentService equipmentService) {
        this.equipmentService = equipmentService;
    }

    /**
     * @Description: 新增/修改设备
     * @Param: [equipmentVo]
     * @return: com.sunnuwell.demo.common.vo.CommonResponse
     * @Author: Mr.Lv
     * @Date: 2019/4/26
     */
    @PostMapping(value = "/addEquipment")
    @UserLoginToken
    public String addEquipment(@RequestBody EquipmentVo equipmentVo) throws AdException, ParseException {
        log.info("EquipmentController: add -> {}",
                JSON.toJSONString(equipmentVo));
        return equipmentService.save(equipmentVo);
    }

    /**
     * @Description: 删除设备
     * @Param: [equipmentVo]
     * @return: com.sunnuwell.demo.common.vo.CommonResponse
     * @Author: Mr.Lv
     * @Date: 2019/4/26
     */
    @DeleteMapping(value = "/deleteEquipment")
    @UserLoginToken
    public String deleteEquipment(@RequestBody EquipmentVo equipmentVo) throws AdException {
        log.info("EquipmentController: delete -> {}",
                JSON.toJSONString(equipmentVo));
        equipmentService.delete(equipmentVo);
        return Constants.ErrorMsg.OPERATION_SUCCESS;
    }

    /**
     * @Description: 设备列表/查询
     * @Param: [equipmentVo]
     * @return: org.springframework.data.domain.Page<com.sunnuwell.demo.entity.Equipment>
     * @Author: Mr.Lv
     * @Date: 2019/4/26
     */
    @ResponseBody
    @PostMapping("/getAllEquipment")
    @UserLoginToken
    public Page<Equipment> getAllEquipment(@RequestBody EquipmentVo equipmentVo) throws AdException {
        log.info("EquipmentController: findAll -> {}",
                JSON.toJSONString(equipmentVo));
        return equipmentService.findAllEquipment(equipmentVo);
    }


}